package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.common.menuitem;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.menu.BaseItem;
import com.gwtext.client.widgets.tree.TreeNode;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.data.EntityData;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.ItemSelector;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.MatchLevel;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.UtilEntityType;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.cutter.CuttingMode;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.cutter.CuttingOptions;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.cutter.CuttingResult;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.cutter.CuttingScope;
import edu.stanford.bmir.protege.web.client.ui.library.msgbox.MessageBox;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.treepanel.AbstractStkosTreePanel;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.common.StkosCutterConstant;
import edu.stanford.bmir.protege.web.shared.entity.OWLEntityData;

public class CutterSubRecordMenuItem extends
		AbstractStkosCutterTreeNodeMenuItem {

	public static final String TEXT = "裁切当前子树";
	public static final String ICONCLS = StkosCutterConstant.CUTTER_ICONCLS;

	public CutterSubRecordMenuItem(TreeNode selectedTreeNode, Project project,
			AbstractStkosTreePanel treePanel) {
		super(TEXT, ICONCLS, selectedTreeNode, project, treePanel);
	}

	@Override
	public void onClick(BaseItem item, EventObject event) {
		cutOntology();
	}

	protected void cutOntology() {
		String entityName = null;
		if (selectedTreeNode.getUserObject() instanceof EntityData) {
			entityName = ((EntityData) selectedTreeNode.getUserObject())
					.getName();
		} else if (selectedTreeNode.getUserObject() instanceof OWLEntityData) {
			entityName = ((OWLEntityData) selectedTreeNode.getUserObject())
					.getEntity().getIRI().toString();
		} else {
			MessageBox.showAlert("提示", "必要数据不完整，无法执行操作!");
			return;
		}
		checkNull(entityName);

		String projectName = project.getProjectId().getId();

		CuttingOptions options = new CuttingOptions();
		options.setCuttingMode(CuttingMode.SEARCH);
		options.setCuttingScope(CuttingScope.SELECTED_CLASS_TREE);
		options.setItemSelector(ItemSelector.MATCHED_ITEMS);
		options.setSelectedEntityType(UtilEntityType.CLASS);
		options.setMatchLevel(MatchLevel.INCLUSIVE);
		options.setSelectedEntityName(entityName);
		options.setSearch("*");

		AsyncCallback<CuttingResult> callback = new AsyncCallback<CuttingResult>() {
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.showErrorMessage(TEXT + "失败", caught);
			}

			@Override
			public void onSuccess(CuttingResult result) {
				selectedTreeNode.expand();
				refreshSelectedTreeNode();
			}
		};
		cutOntology(projectName, options, callback);
	}
}
